package com.yxw.live_vod_boot.service.impl;

import com.yxw.live_vod_boot.entity.YxSysNoticeEntity;
import com.yxw.live_vod_boot.entity.YxSysNoticeSendEntity;
import com.yxw.live_vod_boot.entity.YxVideoInterfaceLogEntity;
import com.yxw.live_vod_boot.mapper.LogMapper;
import com.yxw.live_vod_boot.service.CreateTableService;
import com.yxw.live_vod_boot.service.LogService;
import com.yxw.yxnet_cd_center.common.constant.CommonConstant;
import com.yxw.yxnet_cd_center.common.enums.CreateTableEnum;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.Future;

/**
 * 创建表service
 * @author liuxin
 * @date 2023.09.25
 */
@Slf4j
@Service
public class LogServiceImpl implements LogService {



    @Autowired
    private CreateTableService createTableService;

    @Autowired
    private LogMapper logMapper;


    /**
     * 系统消息推送记录
     * @param result
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @Async("common-executor")
    public void addSysNoticeLog(Object result) {
        try {
            if(null!=result){
                YxSysNoticeSendEntity logEntity = new YxSysNoticeSendEntity();
                Future<BaseResult> future = (Future<BaseResult>) result;
                BaseResult data = future.get();
                YxSysNoticeEntity noticeEntity = (YxSysNoticeEntity) data.getData();
                BeanUtils.copyProperties(noticeEntity, logEntity);
                String resultMsg = data.getResultMsg();
                //没有异常消息就是推送成功
                if(StringUtils.isBlank(resultMsg)){
                    logEntity.setStatus(CommonConstant.ONE);
                }else{
                    logEntity.setStatus(CommonConstant.TWO);
                }
                logEntity.setErrMsg(resultMsg);
                logEntity.setCreateTime(new Date());
                logEntity.setNoticeId(noticeEntity.getNoticeId());
                //创建记录表
                String tableName = createTableService.createTable(CreateTableEnum.YX_SYS_NOTICE_SEND);
                logMapper.addSysNoticeLog(logEntity,tableName);
            }
        }catch (Exception e){
            log.error("系统消息推送记录失败："+e.getMessage());
        }
    }

    /**
     * 火山引擎上传记录
     * @param logEntity
     */
    @Override
    @Async("common-executor")
    public void addUploadLog(YxVideoInterfaceLogEntity logEntity) {
        try {
            if(null!=logEntity){
                //创建记录表
                String tableName = createTableService.createTable(CreateTableEnum.YX_VIDEO_INTERFACE_LOG);
                logMapper.addUploadLog(logEntity,tableName);
            }
        }catch (Exception e){
            log.error("火山引擎上传记录失败："+e.getMessage());
        }
    }
}
